package br.edu.unifei.cct720.trabalho04.principal.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import br.edu.unifei.cct720.trabalho04.gui.componentes.JButtonAbrirArquivo;
import br.edu.unifei.cct720.trabalho04.gui.componentes.JButtonReiniciar;
import br.edu.unifei.cct720.trabalho04.gui.componentes.JButtonSair;
import br.edu.unifei.cct720.trabalho04.gui.componentes.JButtonSalvarArquivo;
import br.edu.unifei.cct720.trabalho04.gui.componentes.JButtonSobre;
import br.edu.unifei.cct720.trabalho04.gui.painel.PanelDesenho;
import br.edu.unifei.cct720.trabalho04.principal.mediator.MediatorPrincipal;
import br.edu.unifei.cct720.trabalho04.utils.formas.Objeto;
import br.edu.unifei.cct720.trabalho04.utils.ponto.Ponto;

/**
 * Classe responsavel pela implementacao 
 * da interface do projeto
 * 
 * @author fknappe
 *
 */
public class JanelaPrincipal extends JFrame {
	
	// Declaracao das variaveis de instancia
	private MediatorPrincipal med;
	
	/**
	 * Construtor
	 * 
	 * @param String title
	 */
	public JanelaPrincipal(String title) {
		super(title);
		med = new MediatorPrincipal(this);
		initComponents();
		med.registraEventos();
	}
	
	/**
	 * Metodo responsavel pela inicializacao e disposicao
	 * dos componentes na GUI
	 * 
	 * @return void
	 */
	public void initComponents() {
		
		java.awt.GridBagConstraints gridBagConstraints;
		
		// Inicializacao dos componentes da GUI
		panelDesenho = new PanelDesenho();
		panelObjetos = new javax.swing.JPanel();
		panelFuncao = new javax.swing.JPanel();
		btnAbrirArquivo = new JButtonAbrirArquivo("Abrir Arquivo...", this.med);
		btnSalvarArquivo = new JButtonSalvarArquivo("Salvar...", this.med);
		btnReiniciar = new JButtonReiniciar("Reiniciar", this.med);
		btnSobre = new JButtonSobre("Sobre", this.med);
		btnSair = new JButtonSair("Sair", this.med);
		
		getContentPane().setLayout(new java.awt.GridBagLayout()); 
		
		//Disposicao dos componentes na GUI
		panelObjetos.setBorder(BorderFactory.createTitledBorder(null, "Objetos", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		getContentPane().add(panelObjetos, gridBagConstraints);
		
		panelDesenho.setBackground(Color.WHITE);
		panelDesenho.setBorder(BorderFactory.createEmptyBorder());
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.weighty = 100.0;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		getContentPane().add(panelDesenho, gridBagConstraints);
		
		panelFuncao.add(btnAbrirArquivo);
		btnSalvarArquivo.setEnabled(false);
		panelFuncao.add(btnSalvarArquivo);
		btnReiniciar.setEnabled(false);
		panelFuncao.add(btnReiniciar);
		panelFuncao.add(btnSobre);
		panelFuncao.add(btnSair);
		
		panelFuncao.setBorder(BorderFactory.createTitledBorder(null, "Menu de Funcoes", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		getContentPane().add(panelFuncao, gridBagConstraints);
		
		this.setExtendedState(MAXIMIZED_BOTH);	
		this.setVisible(true);
	}
	
	// Declaracao dos componentes da GUI
	private PanelDesenho panelDesenho;
	private javax.swing.JCheckBox[] checkObjetos;
	private javax.swing.JPanel panelObjetos;
	private javax.swing.JPanel panelFuncao;
	private JButtonAbrirArquivo btnAbrirArquivo;
	private JButtonSalvarArquivo btnSalvarArquivo;
	private JButtonReiniciar btnReiniciar;
	private JButtonSobre btnSobre;
	private JButtonSair btnSair;
	
	/**
	 * Metodo responsavel por habilitar os botoes
	 * do menu de funcoes
	 * 
	 * @return void
	 */
	public void habilitarBotoes() {
		this.getBtnSalvarArquivo().setEnabled(true);
		this.getBtnReiniciar().setEnabled(true);
	}
	
	/**
	 * Metodo responsavel por desabilitar os botoes
	 * do menu de funcoes
	 * 
	 * @return void
	 */
	public void desabilitarBotoes() {
		this.getBtnSalvarArquivo().setEnabled(false);
		this.getBtnReiniciar().setEnabled(false);
	}
	
	/**
	 * Metodo responsavel por criar o menu de controle
	 * dos objetos da area de trabalho
	 * 
	 * @return void
	 */
	public void inserirObjetos(int qtde) {
		this.panelObjetos.removeAll();
		this.checkObjetos = new JCheckBox[qtde];
		for(int i = 0; i < qtde; i++) {
			checkObjetos[i] = new JCheckBox((i+1)+"");			
			panelObjetos.add(checkObjetos[i]);
		}
	}
	
	// Metodos getters e setters
	public PanelDesenho getPanelDesenho() {
		return panelDesenho;
	}

	public javax.swing.JCheckBox[] getCheckObjetos() {
		return checkObjetos;
	}

	public JButtonAbrirArquivo getBtnAbrirArquivo() {
		return btnAbrirArquivo;
	}

	public JButtonSalvarArquivo getBtnSalvarArquivo() {
		return btnSalvarArquivo;
	}

	public JButtonReiniciar getBtnReiniciar() {
		return btnReiniciar;
	}
	
	public JButtonSobre getBtnSobre() {
		return btnSobre;
	}

	public JButtonSair getBtnSair() {
		return btnSair;
	}
}
